var ns, wxPayPlus, aliPayPlus, toast;
var app = new Vue({
    el: '#app',
    data: {
        paymentlist: [{
            title: '微信支付',
            key: 'wxpay',
            css: 'icon-weixin',
            checked: false
        }
        , {
            title: '支付宝支付',
            key: 'alipay',
            css: 'icon-alipay',
            checked: false
        }
        // , {
        //     title: '积分支付',
        //     key: 'point',
        //     css: 'icon-point',
        //     checked: false
        // }
    ],
        ordersummary: {
            totalCost: 0,
            restPoint: 0,
            allTotalPoint: 0,
            canUsePoint: false,
        },
        paymentid: 0,
        orderid: 0,
        orderidlist: [],
        selected_payment: '',
        show_shiprate: false,
        pwdvalid: false,
        show_shadow: false
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getpayorderlistsummary',
                orderid: that.orderid,
                orderidlist: JSON.stringify(that.orderidlist)
            }, function(succeed, data, err) {
                if (succeed) {
                    that.ordersummary = data;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        select_paymentmethod: function(item) {
            var that = this;
            if (item.key == 'point' && !that.ordersummary.canUsePoint) {
                item.checked = false;
                if (that.ordersummary.allTotalPoint > that.ordersummary.restPoint && that.ordersummary.allTotalPoint > 0) {
                    api.toast({
                        msg: '积分余额不足',
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                else{
                  api.toast({
                      msg: '请选择物业费',
                      duration: 2000,
                      location: 'bottom'
                  });
                }
                return;
            }
            for (var i = 0; i < that.paymentlist.length; i++) {
                that.paymentlist[i].checked = false;
            }
            item.checked = true;
            that.selected_payment = item.key;
        },
        do_pay: function() {
            var that = this;
            if (that.selected_payment == '') {
                api.toast({
                    msg: '请选择付款方式',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.selected_payment == 'wxpay') {
                that.do_weixin_pay();
                return;
            }
            if (that.selected_payment == 'alipay') {
                that.do_alipay();
                return;
            }
            if (that.selected_payment == 'point') {
                that.do_pay_point();
                return;
            }
        },
        do_pay_point: function() {
            var that = this;
            ns.post({
                action: 'dopaypointorder',
                orderid: that.orderid,
                orderidlist: JSON.stringify(that.orderidlist),
                paymentid: that.paymentid
            }, function(succeed, data, err) {
                if (succeed) {
                    that.open_order_complete();
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '支付中'
            });
        },
        do_weixin_pay: function() {
            var that = this;
            ns.post({
                action: 'wxpayorderready',
                orderid: that.orderid,
                orderidlist: JSON.stringify(that.orderidlist),
                paymentid: that.paymentid
            }, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    var options = {
                        apiKey: data.apiKey,
                        orderId: data.orderId,
                        mchId: data.mchId,
                        nonceStr: data.noncestr,
                        timeStamp: data.timestamp,
                        package: data.package,
                        sign: data.sign
                    };       
                    wxPayPlus.payOrder(options, function(ret, err) {
                        if (ret.status) {
                            api.toast({
                                msg: '支付成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.sendEvent({
                                name: 'updatewuyejiaofei'
                            });
                            that.open_order_complete();
                        } else {
                            if (err.code == "-1") {
                                api.toast({
                                    msg: "系统繁忙，请稍后再试",
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            } else if (err.code == "-2") {
                                api.toast({
                                    msg: "用户取消支付",
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            } else {
                                api.toast({
                                    msg: err.msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }
                    });

                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '支付中'
            });
        },
        do_alipay: function() {
            var that = this;
            ns.post({
                action: 'alipayorderready',
                orderid: that.orderid,
                orderidlist: JSON.stringify(that.orderidlist),
                paymentid: that.paymentid,
                ProductCode: 'QUICK_MSECURITY_PAY'
            }, function(succeed, data, err) {
                if (succeed) {
                    aliPayPlus.payOrder({
                        orderInfo: data.body
                    }, function(ret, err) {
                        if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                            return;
                        } else {
                            if (ret.code == "9000") {
                                api.toast({
                                    msg: "支付成功",
                                    duration: 1000,
                                    location: 'bottom'
                                });
                                that.open_order_complete();
                            } else {
                                api.toast({
                                    msg: '系统异常',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                            return;
                        }
                    });
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '支付中'
            });
        },
        open_order_complete: function() {
            var that = this;
            setTimeout(function() {
                that.open_order_complete_process();
            }, 1000);
        },
        open_order_complete_process: function() {
            var that = this;
            ns.openWin('dl_order_detail', '订单完成', {
                title: '订单完成',
                url: 'dl_order_detail.html',
                id: that.orderid,
                orderidlist: JSON.stringify(that.orderidlist),
                type: 7
            }, {
                needlogin: true
            });
        },
        doClose: function() {
            api.closeFrame();
            api.sendEvent({
                name: 'do_close_orderconfirm'
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    wxPayPlus = api.require('wxPayPlus');
    aliPayPlus = api.require('aliPayPlus');
    app.orderid = api.pageParam.id || 0;
    app.paymentid = api.pageParam.paymentid || 0;
    var orderids = api.pageParam.ids || '[]';
    app.orderidlist = eval('(' + orderids + ')');
    app.get_data();
    setTimeout(function() {
        api.sendEvent({
            name: 'on_open_orderpay'
        });
    }, 100);
    api.addEventListener({
        name: 'on_open_ordercomplete'
    }, function() {
        setTimeout(function() {
            api.closeWin();
        }, 500);
    });
    api.addEventListener({
        name: 'close_pwd_pay'
    }, function() {
        app.show_shadow = false;
    });
    api.addEventListener({
        name: 'orderpay_pwdvalid'
    }, function() {
        app.show_shadow = false;
        app.pwdvalid = true;
        app.do_pay();
    });
};
